//
//  NewPersonController.m
//  CoreData
//
//  Created by ZRAR on 16/1/15.
//  Copyright © 2016年 ZRAR. All rights reserved.
//

#import "NewPersonController.h"
#import "TextFieldCell.h"
#import "EditViewModel.h"
#import <ReactiveCocoa/ReactiveCocoa.h>

@interface NewPersonController ()

@property (nonatomic, strong) NSArray *placeholders;

- (IBAction)CancelAction:(id)sender;

- (IBAction)doneAction:(id)sender;

@end

@implementation NewPersonController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.placeholders = @[
                          @"姓名",
                          @"电话",
                          @"地址",
                          @"邮箱"
                          ];
    RAC(self,title) = RACObserve(self.viewModel, name);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.placeholders.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCell" forIndexPath:indexPath];
   
    cell.textField.placeholder = self.placeholders[indexPath.row];
    switch (indexPath.row) {
        case  0:
        {
            cell.textField.text = self.viewModel.name;
            //takeUntil: 当给定的signal完成前一直取值，
            //cell.rac_prepareForReuseSignal，cell即将要重用时，告诉cell.
            RAC(self.viewModel,name) = [cell.textField.rac_textSignal takeUntil:cell.rac_prepareForReuseSignal];
            RAC(self.viewModel,header) = [[[cell.textField.rac_textSignal filter:^BOOL(NSString *value) {
                
                return value.length > 0;
            }]map:^id(NSString *value) {
                
                return [[value substringToIndex:1] uppercaseString];
            }]takeUntil:cell.rac_prepareForReuseSignal];
        }
            break;
         case 1:
        {
            cell.textField.text = self.viewModel.phone;
            RAC(self.viewModel,phone) = [cell.textField.rac_textSignal takeUntil:cell.rac_prepareForReuseSignal];
        }
            break;
        case 2:
        {
            cell.textField.text = self.viewModel.adress;
            RAC(self.viewModel,adress) = [cell.textField.rac_textSignal takeUntil:cell.rac_prepareForReuseSignal];
        }
            break;
        case 3:
        {
            cell.textField.text = self.viewModel.email;
            RAC(self.viewModel,email) = [cell.textField.rac_textSignal takeUntil:cell.rac_prepareForReuseSignal];
        }
        default:
            break;
    }
    
    
    return cell;
}



-(void)dismissSelf {
    [self.viewModel willDismiss];
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)CancelAction:(id)sender {
    
    [self.viewModel cancel];
    [self dismissSelf];
}

- (IBAction)doneAction:(id)sender {
    
    [self dismissSelf];
}
@end
